 //控制层 
app.controller('typeTemplateController' ,function($scope,$controller,typeTemplateService,brandService,specificationService){
	
	$controller('baseController',{$scope:$scope});//继承
	
    //读取列表数据绑定到表单中  
	$scope.findAll=function(){
		typeTemplateService.findAll().success(
			function(response){
				$scope.list=response;
			}			
		);
	}    
	
	//分页
	$scope.findPage=function(){
		typeTemplateService.findPage($scope.paginationConf.currentPage,$scope.paginationConf.itemsPerPage).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}
	
	//查询实体 
	$scope.findOne=function(id){				
		typeTemplateService.findOne(id).success(
			function(response){
				$scope.entity= response;
				//转换品牌、规格、扩展属性列表，把json字符串转换成json对象
                $scope.entity.brandIds = JSON.parse(response.brandIds);//选中品牌下拉框
                $scope.entity.specIds = JSON.parse(response.specIds);//选中规格下拉框
                $scope.entity.customAttributeItems = JSON.parse(response.customAttributeItems);//显示扩展属性


            }
		);				
	}
	
	//保存 
	$scope.save=function(){				
		var serviceObject;//服务层对象  				
		if($scope.entity.id!=null){//如果有ID
			serviceObject=typeTemplateService.update( $scope.entity ); //修改  
		}else{
			serviceObject=typeTemplateService.add( $scope.entity  );//增加 
		}				
		serviceObject.success(
			function(response){
				if(response.success){
					//重新查询 
		        	$scope.findPage();//重新加载
				}else{
					alert(response.message);
				}
			}		
		);				
	}
	
	 
	//批量删除 
	$scope.dele=function(){			
		//获取选中的复选框			
		typeTemplateService.dele( $scope.selectIds ).success(
			function(response){
				if(response.success){
					$scope.findPage();//刷新列表
					$scope.selectIds=[];
				}						
			}		
		);				
	}
	
	$scope.searchEntity={};//定义搜索对象
	//搜索
	$scope.search=function(){
		typeTemplateService.search($scope.paginationConf.currentPage,$scope.paginationConf.itemsPerPage,$scope.searchEntity).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}
    $scope.brandList={data:[]};//品牌列表
	//查询所有品牌
	$scope.findAllBrands = function () {
		brandService.findAllBrands().success(function (response) {
			$scope.brandList ={data:response};
        })
    }
    $scope.specList={data:[]};//规格列表
    //查询所有规格
    $scope.findAllSpecifications = function () {
        specificationService.findAllSpecifications().success(function (response) {
            $scope.specList = {data:response};
        })
    }

    //新建一行扩展属性
    $scope.addItems = function () {
        $scope.entity.customAttributeItems.push({});//添加一行扩展属性
    };
    //删除一行扩展属性
	$scope.delItem =function (index) {
        $scope.entity.customAttributeItems.splice(index,1);//从数组中移除一项
    }

    //将json字符串按照key转换成字符串
	$scope.jsonToString = function (jsonStr,key) {
		//将传入的json字符串转换成json对象
		var str = JSON.parse(jsonStr);
		var info = "";
		for (var i=0;i<str.length;i++){
			info += str[i][key] + "，";
		}
		//删除最后一个"，"
		let result = info.substr(0,info.length-1);
		return result;
    }

});	
